<template>
  <div class="login">
    <van-nav-bar title="会员登录" left-arrow @click-left="$router.go(-1)" />
    <div class="container">
      <div class="title">
        <h3>手机号登录</h3>
        <p>未注册的手机号登录后将自动注册</p>
      </div>

      <div class="form">
        <div class="form-item">
          <input class="inp" maxlength="11" v-model="mobile" placeholder="请输入手机号码" type="text">
        </div>
        <div class="form-item">
          <input class="inp" maxlength="5" v-model="picCode" placeholder="请输入图形验证码" type="text">
          <img :src="picUrl" v-if="picUrl" alt="" @click="getPicUrl">
        </div>
        <div class="form-item">
          <input class="inp" v-model="smsCode" placeholder="请输入短信验证码" type="text">
          <button  @click="getSMSCode" >
          {{ second === totalSecond? '获取验证码': second + '秒后重新发送' }}
          </button>
        </div>
      </div>

      <div class="login-btn" @click="login">登录</div>
    </div>
  </div>
</template>

<script>
import { getPicCode, getSMSCode, login } from '@/api/login'
import { Toast } from 'vant'
export default {
  name: 'LoginPage',
  data () {
    return {
      picCode: '', // 用户输入的图像验证码
      captchaKey: '', // 图形验证码key
      mobile: '', // 手机号
      picUrl: '', // 存储请求渲染的图片地址
      totalSecond: 60, // 总秒数
      second: 60, // 当前秒数，开定时器对 second
      timer: null, // 定时器
      smsCode: '' // 用户输入的短信验证码
    }
  },
  created () {
    this.getPicUrl()
  },
  methods: {
    // 获取图片验证码
    async getPicUrl () {
      const { data: { base64, key } } = await getPicCode()
      if (key) {
        Toast.success('获取图片验证码成功')
      }
      this.captchaKey = key // 存储唯一标识
      this.picUrl = base64 // 存储图片
    },
    // 短信验证码获取校验
    isGetSMSCode () {
      if (!/^1[3-9]\d{9}$/.test(this.mobile)) {
        Toast('请输入完整的手机号')
        return false
      }
      if (!/^\w{4}$/.test(this.picCode)) {
        Toast('请输入完整的验证码')
        return false
      }
      return true
    },
    // 获取短信验证码
    async getSMSCode () {
      if (!this.isGetSMSCode()) {
        return
      }
      // 启动定时器了，并且Second已经和最大值不相等时，不会在执行下一步，节流概念
      if (this.timer && this.second !== this.totalSecond) {
        return
      }
      const data = {
        mobile: this.mobile,
        captchaKey: this.captchaKey,
        captchaCode: this.picCode
      }
      const res = await getSMSCode(data)
      this.second--
      this.timer = setInterval(() => {
        this.second--
        if (this.second <= 0) {
          clearInterval(this.timer)
          this.timer = null
          // 归位，可以重新获取了
          this.second = this.totalSecond
        }
      }, 1000)
      console.log(res)
    },
    // 登录校验
    isLogin () {
      if (!this.isGetSMSCode()) {
        return false
      }
      if (!/^\d{6}$/.test(this.smsCode)) {
        Toast('短信验证码格式不对')
        return false
      }
      return true
    },
    // 登录
    async login () {
      if (!this.isLogin()) {
        return false
      }
      const data = {
        smsCode: this.smsCode,
        mobile: this.mobile,
        isParty: false,
        partyData: {}
      }
      const res = await login(data)
      this.$store.commit('user/setUserInfo', res.data)
      if (this.$route.query.baseUrl) {
        this.$router.replace(this.$route.query.baseUrl)
      } else {
        this.$router.push('/home')
      }
      // console.log(res.data)
    }
  },
  destroyed () {
    // 清除定时器，离开这个页面
    clearInterval(this.timer)
  }
}
</script>

<style lang="less" scoped>
.container {
  padding: 49px 29px;

  .title {
    margin-bottom: 20px;
    h3 {
      font-size: 26px;
      font-weight: normal;
    }
    p {
      line-height: 40px;
      font-size: 14px;
      color: #b8b8b8;
    }
  }

  .form-item {
    border-bottom: 1px solid #f3f1f2;
    padding: 8px;
    margin-bottom: 14px;
    display: flex;
    align-items: center;
    .inp {
      display: block;
      border: none;
      outline: none;
      height: 32px;
      font-size: 14px;
      flex: 1;
    }
    img {
      width: 94px;
      height: 31px;
    }
    button {
      height: 31px;
      border: none;
      font-size: 13px;
      color: #cea26a;
      background-color: transparent;
      padding-right: 9px;
    }
  }

  .login-btn {
    width: 100%;
    height: 42px;
    margin-top: 39px;
    background: linear-gradient(90deg,#ecb53c,#ff9211);
    color: #fff;
    border-radius: 39px;
    box-shadow: 0 10px 20px 0 rgba(0,0,0,.1);
    letter-spacing: 2px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
